package com.simple.mrchip.app;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.simple.mrchip.core.HttpAccess;
import com.simple.mrchip.core.HttpAccessListener;


/**
 * @author mrchip
 */
public class MainActivity extends AppCompatActivity implements HttpAccessListener {

    private Button mButtonRegister;
    private Button mButtonLogin;
    private EditText mTextUser;
    private EditText mTextPassword;
    private HttpAccess httpAccess;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        if (sharedPreferences.contains("UUID") && !sharedPreferences.getString("UUID", "").equals("")) {
            Intent intent = new Intent(MainActivity.this, AppActivity.class);
            startActivity(intent);
            this.finish();
        }
        setContentView(R.layout.activity_main);
        httpAccess = HttpAccess.getInstance();
        httpAccess.setHost(getResources().getString(R.string.server_hostname));
        httpAccess.setResponser("Login", this);
        mButtonLogin = (Button) findViewById(R.id.btLogin);
        mButtonRegister = (Button) findViewById(R.id.btRegister);
        mTextUser = (EditText) findViewById(R.id.txtUserName);
        mTextPassword = (EditText) findViewById(R.id.txtPassword);
        mButtonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        mButtonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                httpAccess.login(mTextUser.getText().toString(), mTextPassword.getText().toString());
            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                Toast.makeText(getBaseContext(), "Username or Password is error", Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    public void getHttpData(String response) {
        if (!response.equals("Error")) {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString("UUID", response);
            editor.putString("UserName", mTextUser.getText().toString());
            editor.apply();
            Intent intent = new Intent(MainActivity.this, AppActivity.class);
            startActivity(intent);
            this.finish();
        } else {
            //Send Error Toast.
            handler.sendEmptyMessage(0);
        }
    }
}
